package flc.ast;

import android.app.Dialog;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.stark.more.MorePrefUtil;
import com.stark.more.MoreUiUtil;
import com.stark.more.about.DefAboutActivity;

import flc.ast.activity.CallActivity;
import flc.ast.activity.GameActivity;
import flc.ast.activity.NeedleActivity;
import flc.ast.activity.SelLevelActivity;
import flc.ast.activity.TurntableActivity;
import flc.ast.databinding.ActivityHomeBinding;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatConstant;
import stark.common.basic.event.EventStatProxy;

public class HomeActivity extends BaseAc<ActivityHomeBinding> implements View.OnClickListener {

    private Dialog myDialog;
    private Dialog mySettingDialog;
    private Dialog mySelDifficultyDialog;
    private ImageView ivSettingsRecom;

    @Override
    protected int getPageType() {
        return EventStatConstant.PAGE_TAB1;
    }

    @Override
    protected int onCreate() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statLaunch(this);
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivMy.setOnClickListener(this);
        mDataBinding.ivStart.setOnClickListener(this);
        mDataBinding.ivCall.setOnClickListener(this);
        mDataBinding.ivGame.setOnClickListener(this);
        mDataBinding.ivTurntable.setOnClickListener(this);
        mDataBinding.ivTwister.setOnClickListener(this);

        //我的弹框
        myDialog();
        //设置弹框
        mySettingDialog();
        //脑筋急转弯选择难度弹框
        mySelDifficultyDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivStart://见缝插针
                startActivity(NeedleActivity.class);
                break;
            case R.id.ivCall://点名神器
                startActivity(CallActivity.class);
                break;
            case R.id.ivGame://2048
                startActivity(GameActivity.class);
                break;
            case R.id.ivTurntable://大转盘
                startActivity(TurntableActivity.class);
                break;
            case R.id.ivTwister://脑际急转弯
                mySelDifficultyDialog.show();
                break;
            case R.id.ivMy://我的
                myDialog.show();
                break;
            //我的弹框
            case R.id.ivDialogMyBack:
                myDialog.dismiss();
                break;
            case R.id.ivSetting://设置
                mySettingDialog.show();
                break;
            case R.id.ivFeedback://意见反馈
                BaseWebviewActivity.openFeedback(this);
                break;
            case R.id.ivAbout://关于我们
                startActivity(new Intent(this, DefAboutActivity.class));
                break;
            case R.id.ivAgreement://用户协议
                BaseWebviewActivity.openAssetTerms(mContext);
                break;
            case R.id.ivPrivacy://隐私政策
                BaseWebviewActivity.openAssetPrivacy(this);
                break;
            //设置弹框
            case R.id.ivDialogSettingBack:
                mySettingDialog.dismiss();
                break;
            case R.id.ivSettingsRecom://个性化推荐
                boolean isOpened = MoreUiUtil.isPersonalRecommendOpened();
                MoreUiUtil.setPersonalRecommendOpened(!isOpened);
                if (isOpened) {
                    ivSettingsRecom.setImageResource(R.drawable.kg);
                } else {
                    ivSettingsRecom.setImageResource(R.drawable.kg1);
                }
                break;
            //脑经急转弯选择难度
            case R.id.ivDialogSelDifficultyBack://返回
                mySelDifficultyDialog.dismiss();
                break;
            case R.id.ivDifficulty1://经典
                SelLevelActivity.kind = 1;
                SelLevelActivity.difficulty = getString(R.string.difficulty_text1);
                startActivity(SelLevelActivity.class);
                mySelDifficultyDialog.dismiss();
                break;
            case R.id.ivDifficulty2://益智
                SelLevelActivity.kind = 1;
                SelLevelActivity.difficulty = getString(R.string.difficulty_text2);
                startActivity(SelLevelActivity.class);
                mySelDifficultyDialog.dismiss();
                break;
            case R.id.ivDifficulty3://简单
                SelLevelActivity.kind = 1;
                SelLevelActivity.difficulty = getString(R.string.difficulty_text3);
                startActivity(SelLevelActivity.class);
                mySelDifficultyDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    //我的弹框
    private void myDialog() {
        myDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_my, null);
        myDialog.setContentView(inflate);
        myDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        lp.height = dm.heightPixels;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView back = inflate.findViewById(R.id.ivDialogMyBack);
        ImageView setting = inflate.findViewById(R.id.ivSetting);
        ImageView feedback = inflate.findViewById(R.id.ivFeedback);
        ImageView about = inflate.findViewById(R.id.ivAbout);
        ImageView agreement = inflate.findViewById(R.id.ivAgreement);
        ImageView privacy = inflate.findViewById(R.id.ivPrivacy);
        back.setOnClickListener(this);
        setting.setOnClickListener(this);
        feedback.setOnClickListener(this);
        about.setOnClickListener(this);
        agreement.setOnClickListener(this);
        privacy.setOnClickListener(this);
        boolean showPersonalRecommend = MorePrefUtil.showPersonalRecommend();
        if (showPersonalRecommend) {
            //显示个性化开关控件
            setting.setVisibility(View.VISIBLE);
        } else {
            //隐藏个性化开关控件
            setting.setVisibility(View.GONE);
        }
    }

    //设置弹框
    private void mySettingDialog() {
        mySettingDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_setting, null);
        mySettingDialog.setContentView(inflate);
        mySettingDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = mySettingDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        lp.height = dm.heightPixels;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView back = inflate.findViewById(R.id.ivDialogSettingBack);
        ivSettingsRecom = inflate.findViewById(R.id.ivSettingsRecom);
        back.setOnClickListener(this);
        ivSettingsRecom.setOnClickListener(this);
        boolean isOpened = MoreUiUtil.isPersonalRecommendOpened();
        if (isOpened) {
            //将控件开关设为开
            ivSettingsRecom.setImageResource(R.drawable.kg1);
        } else {
            //将控件开关设为关
            ivSettingsRecom.setImageResource(R.drawable.kg);
        }
    }

    //脑筋急转弯选择难度弹框
    private void mySelDifficultyDialog() {
        mySelDifficultyDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_sel_difficulty, null);
        mySelDifficultyDialog.setContentView(inflate);
        mySelDifficultyDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = mySelDifficultyDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        lp.height = dm.heightPixels;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView back = inflate.findViewById(R.id.ivDialogSelDifficultyBack);
        ImageView difficulty1 = inflate.findViewById(R.id.ivDifficulty1);
        ImageView difficulty2 = inflate.findViewById(R.id.ivDifficulty2);
        ImageView difficulty3 = inflate.findViewById(R.id.ivDifficulty3);
        back.setOnClickListener(this);
        difficulty1.setOnClickListener(this);
        difficulty2.setOnClickListener(this);
        difficulty3.setOnClickListener(this);
    }

}